package com.ysfxt.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysfxt.auth.AuthContextHolder;
import com.ysfxt.exception.YsfxtException;
import com.ysfxt.mapper.SysWorksMapper;
import com.ysfxt.model.Evaluate;
import com.ysfxt.mapper.EvaluateMapper;
import com.ysfxt.model.SysTeacher;
import com.ysfxt.model.SysWorks;
import com.ysfxt.result.Result;
import com.ysfxt.result.ResultCodeEnum;
import com.ysfxt.service.EvaluateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysfxt.vo.EvaluateParam;
import com.ysfxt.vo.EvaluateVo;
import com.ysfxt.vo.WorksParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ysfxt
 * @since 2024-03-29
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {
    
    @Resource
    private EvaluateMapper evaluateMapper;
    
    @Resource
    private SysWorksMapper sysWorksMapper;
    
    /**
     * 分页查询
     * @param evaluateParam
     * @return
     */
    @Override
    public Result selectEvaluate(EvaluateParam evaluateParam) {
        evaluateParam.setPageNum((evaluateParam.getPageNum()-1)*evaluateParam.getPageSize());
        List<Map<String,Object>> list = evaluateMapper.mySelectPage(evaluateParam);
        return Result.ok(list);
    }
    
    /**
     * 导出
     * @param response
     */
    @Override
    public void exportData(HttpServletResponse response) {
        try {
            //设置响应头信息和其他信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            String fileName = URLEncoder.encode("评价管理", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //查询所有数据
            List<EvaluateVo> evaluateVo =evaluateMapper.myselect();
            //写出
            EasyExcel.write(response.getOutputStream(), EvaluateVo.class)
                    .sheet("评价管理").doWrite(evaluateVo);
        }catch (Exception e){
            e.printStackTrace();
            throw new YsfxtException(ResultCodeEnum.DATA_ERROR);
        }
    }
    
    /**
     * 评委老师查看参赛作品
     * @param worksParam
     * @return
     */
    @Override
    public Result expertReviewList(WorksParam worksParam) {
        //先获取老师是哪个评选范围的
        SysTeacher teacher = AuthContextHolder.getTeacherLoginVo();
        //1是老师账号，不是评委，没有权限
        if(teacher.getIdentity().getCode()==1)
            return Result.build("没有权限", ResultCodeEnum.PERMISSION);
        LambdaQueryWrapper<SysWorks> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(SysWorks::getScope, teacher.getScope());
        wrapper.like(SysWorks::getWorksName,worksParam.getWorksName());
        if(worksParam.getScore()==-1){
            wrapper.eq(SysWorks::getAvgScore,-1);
        }else if(worksParam.getScore()==1){
            wrapper.ge(SysWorks::getAvgScore, 1);
        }
        IPage<SysWorks> page =new Page<>(worksParam.getPageNum(),worksParam.getPageSize());
        sysWorksMapper.selectPage(page, wrapper);
        return Result.ok(page);
    }
    
    /**
     * 评审 ，前端需要把作品id给发过来
     * @param evaluate
     * @return
     */
    @Override
    public Result expertReview(Evaluate evaluate) {
        SysTeacher teacher = AuthContextHolder.getTeacherLoginVo();
        evaluate.setJudgeName(teacher.getName());
        evaluate.setGmtCreate(new Date());
        evaluate.setGmtModified(new Date());
        evaluateMapper.insert(evaluate);
        SysWorks works =new SysWorks();
        works.setAvgScore(evaluate.getScore());
        works.setId(evaluate.getWorkId());
        works.setGmtModified(new Date());
        sysWorksMapper.updateById(works);
        return Result.ok();
    }
}
